Managing the Server Connection

You'll get into trouble if you aren't careful to close down the X server connection when your program terminates. The reason? Most X servers have a limit to the number of client connections which they can serve at one time. When the limit is reached, the server simply refuses to open any new connections, and you'll end up in the error handler staring at an ``Unable to connect'' message. Make sure your program always terminates with a call to close-display, even when it aborts unexpectedly. The best way is to put your event loop inside an unwind-protect form:

...
(unwind-protect 
  (catch :event-loop
    (loop
      (process-next-event display)))  
  (close-display display))
...

One nifty thing about using this technique is that it's always safe to abort out of a damaged program and start over.

Also, you should generally avoid binding global special variables to contact-display objects representing open server connections. It's too easy to lose track of a global variable or, even worse, to garbage a contact-display while it's still open.